﻿using aier.business.logic;
using aier.dataaccess.model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using log4net;
using Swashbuckle.Swagger.Annotations;

namespace aier.wx.api.selfhost.Controllers
{
    //[Authorize]
    [RoutePrefix("api/Home")]
    public class HomeController : ApiController
    {
        public HomeBll homeBll { get; set; }
        public ILog log { get; set; }

        /// <summary>
        /// 获取首页信息
        /// </summary>
        /// <param name="ownerId">负责人</param>
        /// <param name="curPageIndex">当前页</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="order">排序方向asc/desc</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetHomeInfoPagin")]
        public IHttpActionResult GetHomeInfoPagin(Guid ownerId, int curPageIndex, int pageSize, string order)
        {
            PageResult<HomeInfo> homeInfos = homeBll.QueryHomeInfoByPagin(ownerId, curPageIndex, pageSize, order);
            return Ok<PageResult<HomeInfo>>(homeInfos);
        }

        /// <summary>
        /// 获取搜索结果
        /// </summary>
        /// <param name="ownerId">负责人</param>
        /// <param name="condition">条件：手机号码，座机(模糊搜索)，姓名(模糊搜索)</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetHomeInfoSearch")]
        public IHttpActionResult GetHomeInfoSearch(Guid ownerId, string condition)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(condition))
                {
                    PageResult<HomeInfo> homeInfos = homeBll.QueryHomeInfoByCondition(ownerId, condition);
                    return Ok<PageResult<HomeInfo>>(homeInfos);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }
    }
}
